﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using Beyondbit.Desktop.Common;
using RBS.Admin.Models;
using RBS.Common;
using RBS.Common.JsonEntity;
using RBS.Core.Model.Hotel;
using Webdiyer.WebControls.Mvc;
using RBS.Core.Model;

namespace RBS.Admin.Controllers
{
    public class HotelsManagerController : BaseController
    {
        //
        // GET: /HotelsManager/ 
       /* public ActionResult HotelsList()
        {
            List<RBS.Core.Model.Hotel.H_Hotels> hotelses = new RBS.Core.BLL.Hotel.H_Hotels().GetModelList("");
            ViewData.Model = hotelses; 
            return View();
        }*/

        public ActionResult HotelAudit(int hid, int auditStatus)
        {
            new Core.BLL.Hotel.H_Hotels().HotelAudit(hid, auditStatus);
           return  RedirectToAction("HotelsList");
        }


        /// <summary>
        /// 酒店列表
        /// </summary>
        /// <param name="id"></param>
        /// <param name="kword"></param>
        /// <returns></returns>
        public ActionResult HotelsList(int id = 1, string kword = null)
        {
            int pageSize = 10;
            ChkAdminLevel("H_HotelList", RBSEnums.ActionEnum.View.ToString()); //检查权限
            var bll = new Core.BLL.Hotel.H_Hotels();
            var admin = GetAdminInfo(); //取得当前管理员信息
            if (kword != null) kword = kword.Replace("'", "");
            var strTemp = new StringBuilder();
            if (!string.IsNullOrEmpty(kword))
            {
                strTemp.Append(" and (h_name like  '%" + kword + "%' or address like '%" + kword + "%')");
            }
            string strWhere =  strTemp.ToString();
            const string orderby = "H_id desc";
            var startIndex = id;

            int totalItems = 0; //要分页的总记录数
            DataTable dt = bll.GetList(pageSize, startIndex, strWhere, orderby, out totalItems).Tables[0];
            //PagedList构造函数
            var list = IListDataSet.DataTableToIList<RBS.Core.Model.Hotel.H_Hotels>(dt);
            var arts = new PagedList<RBS.Core.Model.Hotel.H_Hotels>(list, id, pageSize, totalItems);
            return View(arts);
        }
        /// <summary>
        /// 用户列表
        /// </summary>
        /// <param name="hid"></param>
        /// <param name="id"></param>
        /// <param name="kword"></param>
        /// <returns></returns>
        public ActionResult HotelsUser(int hid, int id = 1, string kword = null)
        {
            const int pageSize = 10;
            ChkAdminLevel("H_HotelList", RBSEnums.ActionEnum.View.ToString()); //检查权限
            var bll = new Core.BLL.Hotel.H_Hotel_Users();
            var admin = GetAdminInfo(); //取得当前管理员信息
            if (kword != null) kword = kword.Replace("'", "");
            var strTemp = new StringBuilder();
            if (!string.IsNullOrEmpty(kword))
            {
                strTemp.Append(" and (H_UID like  '%" + kword + "%' or H_UserName like '%" + kword + "%')");
            }
            string strWhere = "and HotelId=" + hid + strTemp.ToString();
            const string orderby = " id  desc";
            var startIndex = id;

            int totalItems = 0; //要分页的总记录数
            DataTable dt = bll.GetList(pageSize, startIndex, strWhere, orderby, out totalItems).Tables[0];
            //PagedList构造函数 
            var list = IListDataSet.DataTableToIList<RBS.Core.Model.Hotel.H_Hotel_Users>(dt);
            PagedList<H_Hotel_Users> arts = null;
            if(list!=null)
              arts = new PagedList<RBS.Core.Model.Hotel.H_Hotel_Users>(list, id, pageSize, totalItems);
            else
            {
                list = new List<H_Hotel_Users>();
                arts = new PagedList<RBS.Core.Model.Hotel.H_Hotel_Users>(list, id, pageSize, totalItems);  
            } 
            return View(arts);
        } 

        #region 酒店认证 
        public ActionResult HotelCertification(int hid, int id = 1, string kword = null)
        {
            const int pageSize = 10;
            ChkAdminLevel("#", RBSEnums.ActionEnum.View.ToString()); //检查权限
            var bll =   new RBS.Core.BLL.Hotel.H_Hotel_Certification();  
           // var admin = GetAdminInfo(); //取得当前管理员信息
            if (kword != null) kword = kword.Replace("'", "");
            var strTemp = new StringBuilder();
            if (!string.IsNullOrEmpty(kword))
            {
                //strTemp.Append(" and (H_UID like  '%" + kword + "%' or H_UserName like '%" + kword + "%')");
            }
            string strWhere = " and  HotelId=" + hid + strTemp.ToString();
            const string orderby = " id  desc";
            var startIndex = id;

            ViewBag.hid = hid;
            int totalItems = 0; //要分页的总记录数
            DataTable dt = bll.GetList(pageSize, startIndex, strWhere, orderby, out totalItems).Tables[0];
            //PagedList构造函数 
            var list = IListDataSet.DataTableToIList<RBS.Core.Model.Hotel.H_Hotel_Certification>(dt);
            PagedList<RBS.Core.Model.Hotel.H_Hotel_Certification> arts = null;
            if(list!=null)
              arts = new PagedList<RBS.Core.Model.Hotel.H_Hotel_Certification>(list, id, pageSize, totalItems);
            else
            {
                /*list = new List<RBS.Core.Model.Hotel.H_Hotel_Certification>();
                arts = new PagedList<RBS.Core.Model.Hotel.H_Hotel_Certification>(list, id, pageSize, totalItems); */
                return RedirectToAction("HotelCertificationEdit", new RouteValueDictionary { { "hid", hid }});
               
            }
            var hotlsModel = new RBS.Core.BLL.Hotel.H_Hotels().GetModelByCache(hid);
            ViewBag.HotelName = hotlsModel.H_Name;
            return View(arts);
        }
         
         //edit
         public ActionResult HotelCertificationEdit(int hid, int id = 0)
         {
             var model = new RBS.Core.Model.Hotel.H_Hotel_Certification {HotelId = hid,CertificationTime = DateTime.Now};
             if (id != 0)
             { 
                 var bll = new RBS.Core.BLL.Hotel.H_Hotel_Certification();
                 model = bll.GetModel(id);
             }

             var hotlsModel = new RBS.Core.BLL.Hotel.H_Hotels().GetModelByCache(hid);
             ViewBag.HotelName = hotlsModel.H_Name;
             ViewData.Model = model;
             return View();
         }
         [HttpPost]
         public ActionResult HotelCertificationEdit(FormCollection collection)
         {

           
             var model = new RBS.Core.Model.Hotel.H_Hotel_Certification
             {
                 HotelId = Convert.ToInt32( collection["HotelId"]),
                 IsCertification = collection["IsCertification"]=="1"?1:0,
                 CertificationTime = DateTime.Parse( collection["CertificationTime"]),
                 CertificationYear =Convert.ToInt32(collection["CertificationYear"]) ,
                 CertificationMoney =Convert.ToDecimal( collection["CertificationMoney"]) 
             };
             
             var bll = new RBS.Core.BLL.Hotel.H_Hotel_Certification();
             if (collection["Id"] != "0")
             {
                 model.Id = Convert.ToInt32(collection["Id"]);
                 bll.Update(model);
                 AddAdminLog(RBSEnums.ActionEnum.Edit.ToString(), "修改:" + model.Id); //记录日志
             }
             else
             { 
                 bll.Add(model); 
                 var model1 = new RBS.Core.Model.Hotel.H_Hotel_Recognizance
                 {
                     HotelId = int.Parse(collection["HotelId"]),
                     Money = Convert.ToDecimal( collection["CertificationMoney"]),
                     Time = DateTime.Now,
                     RType = 1
                 };
              
                 new RBS.Core.BLL.Hotel.H_Hotel_Recognizance().Add(model1);
                 new RBS.Core.BLL.Hotel.H_Hotels().HotelAudit(model.HotelId, 4);
                 AddAdminLog(RBSEnums.ActionEnum.Add.ToString(), "添加:" + model.Id); //记录日志
             }
             return RedirectToAction("HotelCertification",new RouteValueDictionary{{"hid",model.HotelId},{"abc","123"}});
         }


         [AcceptVerbs(HttpVerbs.Post)]
         public JsonResult HotelCertificationDelete(string ids, string returnUrl)
         {
             var ret = new JsonReturnMessages();
             ChkAdminLevel("#", RBSEnums.ActionEnum.Delete.ToString()); //检查权限 
             try
             {
                 foreach (var id in ids.TrimEnd(',').Split(',').ToList())
                 {
                     var bll = new RBS.Core.BLL.Hotel.H_Hotel_Certification();
                     var model = bll.GetModel(int.Parse(id));
                     new RBS.Core.BLL.Hotel.H_Hotel_Certification().Delete(model.Id);
                     new RBS.Core.BLL.Hotel.H_Hotels().HotelAudit(model.HotelId, 3);
                 }
             }
             catch (Exception exception)
             {
                 ret.IsSuccess = false;
                 ret.Msg = "操作失败！";
                 return Json(ret);
             }
             AddAdminLog(RBSEnums.ActionEnum.Delete.ToString(), "删除"); //记录日志
             ret.IsSuccess = true;
             ret.Msg = "操作成功";
             ret.Data = !string.IsNullOrEmpty(returnUrl) ? returnUrl : Url.Action("AdminUserManager", "Sys");
             return Json(ret);
         } 
        #endregion 

        #region 保证金管理 
         //list
         public ActionResult HotelRecognizance(int ids=0, int id = 1, string kword = null)
        {
            const int pageSize = 10;
            ChkAdminLevel("#", RBSEnums.ActionEnum.View.ToString()); //检查权限
            var bll =    new RBS.Core.BLL.Hotel.H_Hotel_Recognizance();  
           // var admin = GetAdminInfo(); //取得当前管理员信息
            if (kword != null) kword = kword.Replace("'", "");
            var strTemp = new StringBuilder();
            if (!string.IsNullOrEmpty(kword))
            {
                strTemp.Append(" and  HotelId in ( select H_id from H_Hotels where H_Name   like  '%" + kword + "%') or  HotelId ='" + kword + "' ");
            }
            string strWhere =  strTemp.ToString();
            const string orderby = " id  desc";
            var startIndex = id;

            int totalItems = 0; //要分页的总记录数
            DataTable dt = bll.GetList(pageSize, startIndex, strWhere, orderby, out totalItems).Tables[0];
            //PagedList构造函数 
            var list = IListDataSet.DataTableToIList<RBS.Core.Model.Hotel.H_Hotel_Recognizance>(dt);
            PagedList<RBS.Core.Model.Hotel.H_Hotel_Recognizance> arts = null;
            if(list!=null)
              arts = new PagedList<RBS.Core.Model.Hotel.H_Hotel_Recognizance>(list, id, pageSize, totalItems);
            else
            {
                list = new List<RBS.Core.Model.Hotel.H_Hotel_Recognizance>();
                arts = new PagedList<RBS.Core.Model.Hotel.H_Hotel_Recognizance>(list, id, pageSize, totalItems);  
            } 
            return View(arts);
        }
         //edit
         public ActionResult HotelRecognizanceEdit(int id = 0,int hid=0)
         {
             var model = new RBS.Core.Model.Hotel.H_Hotel_Recognizance {HotelId = hid, Time = DateTime.Now};
             if (id != 0)
             { 
                 var bll = new RBS.Core.BLL.Hotel.H_Hotel_Recognizance();
                 model = bll.GetModel(id);
             }
            
             ViewData.Model = model;
             return View();
         }
         [HttpPost]
         public ActionResult HotelRecognizanceEdit(FormCollection collection)
         { 
             var model = new RBS.Core.Model.Hotel.H_Hotel_Recognizance
             {
                 HotelId = int.Parse(collection["HotelId"]),
                 Money = decimal.Parse(collection["Money"]),
                 Time = DateTime .Parse(collection["Time"]) ,
                 RType = int.Parse(collection["RType"])
             }; 
             var bll = new RBS.Core.BLL.Hotel.H_Hotel_Recognizance();
             if (collection["Id"] != "0")
             {
                 model.Id = Convert.ToInt32(collection["Id"]);
                 bll.Update(model);
                 AddAdminLog(RBSEnums.ActionEnum.Edit.ToString(), "修改:" + model.HotelId+"消费金"); //记录日志
             }
             else
             { 
                 bll.Add(model);
                 AddAdminLog(RBSEnums.ActionEnum.Add.ToString(), "添加:" + model.Id + "消费金"); //记录日志
             }
             return RedirectToAction("HotelRecognizance");
         }
         
         [AcceptVerbs(HttpVerbs.Post)]
         public JsonResult HotelRecognizanceDelete(string ids, string returnUrl)
         {
             var ret = new JsonReturnMessages();
             ChkAdminLevel("#", RBSEnums.ActionEnum.Delete.ToString()); //检查权限 
             try
             {
                 foreach (var id in ids.TrimEnd(',').Split(',').ToList())
                 {
                     new RBS.Core.BLL.Hotel.H_Hotel_Recognizance().Delete(int.Parse(id));
                 }
             }
             catch (Exception exception)
             {
                 ret.IsSuccess = false;
                 ret.Msg = "操作失败！";
                 return Json(ret); 
             }
             AddAdminLog(RBSEnums.ActionEnum.Delete.ToString(), "删除"); //记录日志
             ret.IsSuccess = true;
             ret.Msg = "操作成功";
             ret.Data = !string.IsNullOrEmpty(returnUrl) ? returnUrl : Url.Action("AdminUserManager", "Sys");
             return Json(ret);
         }

        //---
         [AcceptVerbs(HttpVerbs.Post)]
         public JsonResult UpdateRecommend(string id, string isRecommend)
         {
             var ret = new JsonReturnMessages();
             ChkAdminLevel("#", RBSEnums.ActionEnum.Edit.ToString()); //检查权限 
             int h_Id = Int32.Parse(id);
             try
             {
                 H_Hotels model = new RBS.Core.BLL.Hotel.H_Hotels().GetModel(h_Id);
                 model.H_id = h_Id;
                 model.IsRecommend = Int32.Parse(isRecommend);
                 new RBS.Core.BLL.Hotel.H_Hotels().Update(model);
             }
             catch (Exception exception)
             {
                 ret.IsSuccess = false;
                 ret.Msg = "操作失败！";
                 return Json(ret);
             }
             AddAdminLog(RBSEnums.ActionEnum.Edit.ToString(), "修改状态"); //记录日志
             ret.IsSuccess = true;
             ret.Msg = "操作成功";
             ret.Data = Url.Action("HotelsList", "HotelsManager");
             return Json(ret);
         }
 
        public ActionResult HotelView(int id)
        {
            var hotel = new Hotel();
            hotel.Hotels = new RBS.Core.BLL.Hotel.H_Hotels().GetModelByCache(id);
            hotel.Certification = new RBS.Core.BLL.Hotel.H_Hotel_Certification().GetModelByHotelId(hotel.Hotels.H_id);  
            return View(hotel);
        }
         
        #endregion
            
        #region

        public ActionResult HotelRate()
        {
            return View();
        }

        #endregion
    }
}
